﻿namespace LogManager.Utils.Unity
{
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.InterceptionExtension;
    using NUnit.Framework;
    using System.Linq;

    public class TestUnityHelpers
    {
        [Test]
        public void UnityHelpers_RegisterClass_RegisterSimpleClass_ClassIsRegistred()
        {
            var container = new UnityContainer();

            container.RegisterForInterception<TestClass>(new InterceptionBehavior<PropertyChangedBehavior>());

            var isRegistred = container.IsRegistered<TestClass>();
            var registration = container.Registrations.SingleOrDefault(p => p.RegisteredType == typeof(TestClass) && p.MappedToType == typeof(TestClass));

            Assert.IsTrue(isRegistred);
            Assert.IsTrue(registration != null);
        }

        [Test]
        public void UnityHelpers_RegisterClass_ResolveInstanceOneParameter_InstanceResolved()
        {
            var container = new UnityContainer();
            container.RegisterType<TestClass>();

            var instance = container.Resolve<string, TestClass>("tata");
            Assert.AreEqual(instance.Param1, "tata");
        }

        [Test]
        public void UnityHelpers_RegisterClass_ResolveInstanceTwoParameters_InstanceResolved()
        {
            var container = new UnityContainer();
            container.RegisterType<TestClassTwoParams>();

            var instance = container.Resolve<string, long, TestClassTwoParams>("tata", 85);
            Assert.AreEqual(instance.Param1, "tata");
            Assert.AreEqual(instance.Param2, 85);
        }

        public class TestClass
        {
            public string Param1 { get; set; }

            public TestClass(string param1)
            {
                this.Param1 = param1;
            }
        }

        public class TestClassTwoParams
        {
            public string Param1 { get; set; }
            public long Param2 { get; set; }

            public TestClassTwoParams(string param1, long param2)
            {
                this.Param1 = param1;
                this.Param2 = param2;
            }
        }
    }
}